<?php
namespace Models\Model;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
class UserMedicineOrderTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function saveOrder($order,$prescription_details,$images)
    {
		if(isset($order['eachDays'])){
			$eachDays=$order['eachDays'];
		}else{
			$eachDays=0;
		}
		if(isset($order['availabletime'])){
			$available_time=$order['availabletime'];
		}else{
			$available_time='';
		}
		$data = array(
			'user_id' 	  	       		=> $order['user_id'], 				
			'prescription_details' 		=> $prescription_details,  		
			'prescription_upload_path' 	=> $images,		
			'auto_renewal'  	        => $order['auto_renewal'],  	
			'each_days' 	           	=> $eachDays,
			'immediate' 	            => $order['immediate'],
			'availabletime' 	       	=> $available_time,
			'date_added'	  	       	=> date('Y-m-d H:i:s'), 	
			'date_updated' 		       	=> date('Y-m-d H:i:s'),   			
			'status' 		       		=> 0, 			
		);
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;		
    }	
	public function checkOrder($order_id,$user_id)
    {	
		$select = $this->tableGateway->getSql()->select();			
		$select->where('user_medicine_order_id="'.$order_id.'"');
		$select->where('user_id="'.$user_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();		
		return $row;		
	}
	public function checkOrder_status($order_id)
    {	
		$select = $this->tableGateway->getSql()->select();			
		$select->where('user_medicine_order_id="'.$order_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();		
		return $row;		
	}
	public function orderStatus($order_id){ 
		$select = $this->tableGateway->getSql()->select();
		$select->where('user_medicine_order_id="'.$order_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;
	}
	public function getOrderDetails($order_id){ 
		$select = $this->tableGateway->getSql()->select();
		$select->where('user_medicine_order_id="'.$order_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;
	}
	public function totalOrders($user_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('order_delivery_details', new Expression('order_delivery_details.user_medicine_order_id=user_medicine_order.user_medicine_order_id'),array('price','discount','shop_id','payment_status','delivered_date','shop_order_status'),'left');
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('address','landmark','alterphone'),'left');
		$select->where('user_medicine_order.user_id="'.$user_id.'"');
		$select->where('user_medicine_order.auto_renewal!="1"');
		$select->group('user_medicine_order.user_medicine_order_id');
		$select->order('user_medicine_order.user_medicine_order_id DESC');
		$alllist = $this->tableGateway->selectWith($select);
		return $alllist;
	}
	public function updateOrderRenewal($orderId,$auto_renewal)
    {	
		$data = array(
			'auto_renewal' 	=> $auto_renewal, 
			'date_updated'	=> date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_medicine_order_id' => $orderId));
		return 	$updaed_status;			
	}
	public function cancelledOrder($orderId)
    {	
		$data = array(
			'status' 		=> 3, 
			'date_updated'	=> date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_medicine_order_id' => $orderId));
		return 	$updaed_status;			
	}
	public function getAllUserOrders(){
		$select = $this->tableGateway->getSql()->select();
		$select->join('order_delivery_details', new Expression('order_delivery_details.user_medicine_order_id=user_medicine_order.user_medicine_order_id'),array('price','discount','shop_id','payment_status','delivered_date'),'left');
		$select->join('user', new Expression('user.user_id=order_delivery_details.shop_id'),array('shop_id'=>'user_id','shop_name'=>'user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');
		$select->where('user_medicine_order.status!="2"');
		$select->group('user_medicine_order.user_medicine_order_id');
		$select->order('user_medicine_order.user_medicine_order_id DESC');
		$result = $this->tableGateway->selectWith($select);	
		return $result;
	}
	public function getAllAdminOrders($status){
		$select = $this->tableGateway->getSql()->select();
		$select->join('order_delivery_details', new Expression('order_delivery_details.user_medicine_order_id=user_medicine_order.user_medicine_order_id'),array('price','discount','shop_id','payment_status','delivered_date'),'left');
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('address','location','pincode','landmark','alterphone','phone'),'left');
		if($status=='all'){
			$select->where('user_medicine_order.auto_renewal="0"');
			$select->where('user_medicine_order.status!="0"');
		}else{
			$select->where('user_medicine_order.status="'.$status.'"');
		}
		$select->group('user_medicine_order.user_medicine_order_id');
		$select->order('user_medicine_order_id DESC');
		$result = $this->tableGateway->selectWith($select);		
		return $result;
	}

	public function getAllRenewedOrders()
	{
		$select = $this->tableGateway->getSql()->select();
		$select->join('order_delivery_details', new Expression('order_delivery_details.user_medicine_order_id=user_medicine_order.user_medicine_order_id'),array('price','discount','shop_id','payment_status','delivered_date'),'left');
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('location','pincode','phone','alterphone','landmark','address'),'left');
		$select->where('user_medicine_order.auto_renewal!="0"');
		$select->group('user_medicine_order.user_medicine_order_id');
		$select->order('user_medicine_order_id DESC');
		//$select->where('user_medicine_order.status="1"');
		//$select->where('user_medicine_order.each_days=DATEDIFF(now(),user_medicine_order.date_added)+1');
		$result = $this->tableGateway->selectWith($select);	
		return $result;
	}
	public function changeAutoRenewal($orderId,$order)
	{
	
		$data = array(
			'auto_renewal' 		=> $order['auto_renewal'], 
			'date_updated'		=> date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_medicine_order_id' => $orderId));
		return 	$updaed_status;
	}
	public function autorenuwalorders($user_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('order_delivery_details', new Expression('order_delivery_details.user_medicine_order_id=user_medicine_order.user_medicine_order_id'),array('price','discount','shop_id','payment_status','delivered_date'),'left');
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('address','landmark','alterphone'),'left');
		$select->where('user_medicine_order.user_id="'.$user_id.'"');
		$select->where('auto_renewal="1"');
		$select->group('user_medicine_order.user_medicine_order_id');
		$select->order('user_medicine_order.user_medicine_order_id DESC');
		$alllist = $this->tableGateway->selectWith($select);	
		return $alllist;
	}
	public function getuserorder($data){
		$order_tracking_id=$data['0'];
		$select = $this->tableGateway->getSql()->select();
		if(isset($data['2'])){
			$select->join('order_delivery_details', new Expression('order_delivery_details.user_medicine_order_id=user_medicine_order.user_medicine_order_id'),array('order_delivery_details_id','price','discount','shop_id','payment_status','delivered_date'),'left');
		}
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_name','email'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('address','landmark','alterphone','phone'),'left');
		$select->where('user_medicine_order.user_medicine_order_id="'.$order_tracking_id.'"');
		if(isset($data['2'])){
			$shop_id=$data['2'];
			$select->where('order_delivery_details.shop_id="'.$shop_id.'"');
		}
		$alllist = $this->tableGateway->selectWith($select);
		// echo "<pre>";print_r($alllist);exit
		return $alllist;
	}
	public function changeStatusOrder($orderId)
    {	
		$data = array(
			'status' 		=> 1, 
			'date_updated'	=> date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_medicine_order_id' => $orderId));
		return 	$updaed_status;			
	}
	public function getSentQuatationOrders($mOrderId){
		$select = $this->tableGateway->getSql()->select();
		$select->join('order_delivery_details', new Expression('order_delivery_details.user_medicine_order_id=user_medicine_order.user_medicine_order_id'),array('price','discount','shop_id','payment_status','delivered_date','date_updated','order_delivery_details_id','shop_order_status'),'left');
		$select->join('user', new Expression('user.user_id=order_delivery_details.shop_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('phone','alterphone','address'),'left');
		$select->where('user_medicine_order.user_medicine_order_id="'.$mOrderId.'"');
		$select->order('user_medicine_order.user_medicine_order_id DESC');
		$result = $this->tableGateway->selectWith($select);	
		return $result;
	}
	public function getTommorowRenewedOrders()
	{
		$select = $this->tableGateway->getSql()->select();
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('location','pincode','phone','alterphone','landmark','address'),'left');
		$select->where('user_medicine_order.auto_renewal!="0"');
		$select->where('user_medicine_order.each_days=DATEDIFF(now(),user_medicine_order.date_added)+1');
		$select->order('user_medicine_order_id DESC');
		$result = $this->tableGateway->selectWith($select);	
		return $result;
	}
	public function saveReOrder($order)
    {
		$data = array(
			'user_id' 	  	       		=> $order['user_id'], 				
			'prescription_details' 		=> $order['prescription_details'],  		
			'prescription_upload_path' 	=> $order['prescription_upload_path'],		
			'auto_renewal'  	        => $order['auto_renewal'],  	
			'each_days' 	           	=> $order['each_days'],
			'immediate' 	            => $order['immediate'],
			'availabletime' 	       	=> $order['availabletime'],
			'date_added'	  	       	=> date('Y-m-d H:i:s'), 	
			'date_updated' 		       	=> date('Y-m-d H:i:s'),   			
			'status' 		       		=> 0, 			
		);		
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;		
    }
	public function changeAutoRenewalReorder($orderId)
	{
		$data = array(
			'auto_renewal' 		=> 2, 
			'status' 			=> 2, 
			'date_updated'		=> date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_medicine_order_id' => $orderId));
		return 	$updaed_status;
	}
	public function updateMedicineStatus($orderId)
    {	
		$data = array(
			'status' 		=> 2, 
			'date_updated'	=> date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_medicine_order_id' => $orderId));
		return 	$updaed_status;			
	}
	public function userPendingorders($u_id){
		$select = $this->tableGateway->getSql()->select();			
		$select->where('status="0"');
		$select->where('user_id="'.$u_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();		
		return $row;	
	}
	public function userProcessingorders($u_id){
		$select = $this->tableGateway->getSql()->select();			
		$select->where('status="1"');
		$select->where('user_id="'.$u_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();		
		return $row;	
	}
	public function userProcessedorders($u_id){
		$select = $this->tableGateway->getSql()->select();			
		$select->where('status="2"');
		$select->where('user_id="'.$u_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();		
		return $row;	
	}
	public function userCancelledorders($u_id){
		$select = $this->tableGateway->getSql()->select();			
		$select->where('status="3"');
		$select->where('user_id="'.$u_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();		
		return $row;	
	}
	public function getOrders($orderId,$order)
	{
		$select = $this->tableGateway->getSql()->select();
		$select->where('user_medicine_order_id="'.$orderId.'"');
		$result = $this->tableGateway->selectWith($select);	
		return $result;
	}
	public function getDatewiseReports($data,$status){
		$fdate = date_create($data['from_date']);
		$from_date=date_format($fdate, 'Y-m-d');
		if(isset($data['to_date']) && $data['to_date']!=''){
			$tdate = date_create($data['to_date']);
			$to_date=date_format($tdate, 'Y-m-d');
		}
		$select = $this->tableGateway->getSql()->select();
		if((isset($from_date) && $from_date!='') && (isset($to_date) && $to_date!='')){
			$select->where('date_added>="'.$from_date.'"');
			$select->where('date_added<="'.$to_date.'"');
		}else if((isset($from_date) && $from_date!='')){
			$select->where(array(
				new Predicate\PredicateSet(
					array(
						new Predicate\Like('date_added', '%'.$from_date.'%'),	
					),
					Predicate\PredicateSet::COMBINED_BY_OR
				),
			));					
		}		
		if($status==0){
			$select->where('status="'.$status.'"');
		}else if($status==1){
			$select->where('status="'.$status.'"');
		}else if($status==2){
			$select->where('status="'.$status.'"');
		}else if($status==3){
			$select->where('status="'.$status.'"');
		}
		$resultSet = $this->tableGateway->selectWith($select);	
		$row = $resultSet->count();
		return $row;
	}
	public function getUserId($order_id)
	{
		$select = $this->tableGateway->getSql()->select();
		$select->where('user_medicine_order_id="'.$order_id.'"');
		$result = $this->tableGateway->selectWith($select);	
		return $result;
	}
	public function updatePresImages( $orderId,$newImageName,$imgNumber )
    {
		$presImagesSql = "";
		if( $imgNumber == 1 )
		{
			$presImagesSql = "UPDATE user_medicine_order SET prescription_upload_path= CONCAT(prescription_upload_path, '','$newImageName'),date_updated=now() WHERE user_medicine_order_id= '$orderId'";
		}
		else
		{
			$presImagesSql = "UPDATE user_medicine_order SET prescription_upload_path= CONCAT(prescription_upload_path, '**','$newImageName'),date_updated=now() WHERE user_medicine_order_id= '$orderId'";
		}
		$updateStatus=$this->tableGateway->getAdapter()->driver->getConnection()->execute($presImagesSql);
		return 	$updateStatus;			
	}
	public function getuserorderReorder($data){
		$order_tracking_id  = $data;
		
		$select = $this->tableGateway->getSql()->select();
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_name','email'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('address','landmark','alterphone','phone'),'left');
		$select->where('user_medicine_order.user_medicine_order_id="'.$order_tracking_id.'"');
		$alllist = $this->tableGateway->selectWith($select);
		// echo "<pre>";print_r($alllist);exit
		return $alllist;
	}
}